//
//  SLVisitorView.swift
//  Sina15
//
//  Created by 盼 on 16/4/6.
//  Copyright © 2016年 pan. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol SLVisitorViewDelegate: NSObjectProtocol{
    
    //用户注册方法
    optional func userWillRegister()
    
    //用户登录方法
    optional func userWillLogin()
}

class SLVisitorView: UIView {
    
    //给访客视图设置代理,swift中属性默认的修饰都是strong，代理要设置为weak，防止循环引用
    weak var delegate:SLVisitorViewDelegate?

    //重写父类构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //对外提供设置界面的方法,参数设置默认值后可以不传递
    func setupViewInfo(isHome:Bool = false,title:String,imageName:String){
        
        if isHome {
            
            //是首页的话，开始动画
            startAnimating()
        }else{
            houseIcon.hidden = true
            
            //把圈圈放到视图的最上层
            bringSubviewToFront(circleIcon)
        }
        
        tipsLabel.text = title
        
        circleIcon.image = UIImage(named: imageName)
    }
    
    
    //开启动画
    func startAnimating(){
        
        //创建基本动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        //设置重复次数
        animation.repeatCount = MAXFLOAT
        
        //设置动画持续时间
        animation.duration = 15
        
        //设置旋转角度
        animation.toValue = M_PI * 2
        
        //动画执行结束或者处于非活跃的状态下，动画不移除
        animation.removedOnCompletion = false
        
        //把动画添加到layer上,forKey的参数为nil表示不获取动画
        circleIcon.layer.addAnimation(animation, forKey: nil)
    }
    
    //MARK:设置UI界面
   private func setupUI(){

        addSubview(circleIcon)
        
        //为circleIcon添加约束
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        //添加遮罩
        addSubview(coverView)
        coverView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(circleIcon.snp_center)
        }
        
        //为houseIcon添加约束
        addSubview(houseIcon)
        houseIcon.snp_makeConstraints { (make) -> Void in
        
            make.center.equalTo(circleIcon.snp_center)
        }
        
        //为tipsLabel添加约束
        addSubview(tipsLabel)
        tipsLabel.snp_makeConstraints { (make) -> Void in
            
            make.width.equalTo(230)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.height.equalTo(40)
        }
        
        //为注册按钮添加约束
        addSubview(registerBtn)
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            
            make.width.equalTo(100)
            make.left.equalTo(tipsLabel.snp_left)
            make.top.equalTo(tipsLabel.snp_bottom).offset(16)
            
        }
        
        //为登录按钮添加约束
        addSubview(loginBtn)
        loginBtn.snp_makeConstraints { (make) -> Void in
            
            make.width.equalTo(100)
            make.right.equalTo(tipsLabel.snp_right)
            make.top.equalTo(tipsLabel.snp_bottom).offset(16)
        }
        
        //设置背景色
        let value: CGFloat = 237 / 255.0
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1.0)
    
    //给注册按钮添加点击事件
    registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
    
    //给登录按钮添加点击事件
    loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
    
    }
    
    //注册按钮的点击事件
   @objc private func registerBtnDidClick(){
        
        delegate?.userWillRegister?()
    }
    
    //登录按钮的点击事件
   @objc private func loginBtnDidClick(){
        
        delegate?.userWillLogin?()
    }
    
    
    //MARK:苹果提供手写代码添加约束
    func setupUI2(){
        
        addSubview(circleIcon)
        
        //VFL手写代码添加约束
        //默认frame布局是开启，应该关闭
        circleIcon.translatesAutoresizingMaskIntoConstraints = false
        
        self.addConstraint(NSLayoutConstraint(item: circleIcon, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: circleIcon, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
    }
    
    //懒加载圈圈图标
    private lazy var circleIcon:UIImageView = {
        
        let circleImageView = UIImageView()
        
        circleImageView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return circleImageView
        
    }()
    
    
    //加载遮罩
    private lazy var coverView:UIImageView = {
        
        let cover = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return cover
    }()
    
    
    //懒加载房子图标
    private lazy var houseIcon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //设置label
//    private lazy var tipsLabel:UILabel = {
//       
//        let label = UILabel()
//        label.text = "关注一些人，回到这里看看有什么惊喜"
//        label.textColor = UIColor.darkGrayColor()
//        label.font = UIFont.systemFontOfSize(14)
//        label.textAlignment = .Center
////        label.preferredMaxLayoutWidth = 230
//        label.numberOfLines = 0
//        
//        return label
//    }()
    private lazy var tipsLabel: UILabel = UILabel(labelText: "关注一些人", color: UIColor.darkGrayColor(), fontsize: 14)
    
    
    //懒加载登录按钮
    private lazy var loginBtn:UIButton = {
       
        let btn = UIButton()
        
        //设置按钮上title
        btn.setTitle("登录", forState: .Normal)
        
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        return btn
    }()
    
    //懒加载注册按钮
    private lazy var registerBtn:UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        return btn
    }()
}
